<?php


namespace app\common\model;


use think\Model;

class ProtocolAirlineModel extends Model
{
    protected $name = 'protocol_airline';


    /**
     * 获取地区列表
     * @return array
     */
    public function getAreaList(): array
    {
        try {
            $province = $this->where(['status' => 1])->group('province')->column('province');
            $city = $this->where(['status' => 1])->group('city')->column('city');
            $area = $this->where(['status' => 1])->group('area')->column('area');
            $list = model('app\common\model\Area')
                ->with(['children' => function($q2) use($city,$area){
                    $q2->field('id,pid,name');
                    $q2->whereIn('id',$city);
                    $q2->with(['children' => function($q3) use($area){
                        $q3->field('id,pid,name');
                        $q3->whereIn('id',$area);
                    }]);
                }])
                ->field('id,pid,name')
                ->where(['id' => ['in',$province]])
                ->select();
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

    /**
     * 获取列表
     * @param string $area_mold 省市区类型:all,province,city,area
     * @param string|int $area_id 省市区id
     * @param string $search 搜索
     * @return array
     */
    public function getPageList($params, $user_id, $page, $limit): array
    {
        extract($params);
        try {
            $where = [
                'status' => 1
            ];
            if(isset($area_mold) && $area_mold != 'all' && !empty($area_id)) {
                $where[$area_mold] = $area_id;
            }
            if(isset($search) && !empty($search)) {
                $where['title'] = ['like',"%{$search}%"];
            }
            $field = 'id,title,desc,image,address,lat,lng,tel,tags';
            $order = [
                'createtime' => 'desc',
            ];
            $list = $this
                ->where($where)
                ->field($field)
                ->order($order)
                ->paginate($limit, false, ['page' => $page]);
            foreach ($list as $row) {
                $row->image = wdsxh_full_url($row->image);
                $row->tags = $row->tags?explode(',',$row->tags):[];
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }



    /**
     * 获取详情
     * @return array
     */
    public function getDetail($id): array
    {
        try {
            $data = $this->get($id);
            $data->image = wdsxh_full_url($data->image);
            $data->tags = $data->tags?explode(',',$data->tags):[];
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $data
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }





    public function province(){
        return $this->belongsTo('app\common\model\Area','province','id');
    }


    public function city(){
        return $this->belongsTo('app\common\model\Area','city','id');
    }


    public function area(){
        return $this->belongsTo('app\common\model\Area','area','id');
    }

}